/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.header;

import javax.sip.InvalidArgumentException;
import javax.sip.header.ContentLengthHeader;

/**
 * The Content-Length header field indicates the size of the message-body, in
 * decimal number of octets, sent to the recipient. Applications SHOULD use
 * this field to indicate the size of the message-body to be transferred,
 * regardless of the media type of the entity.  If a stream-based protocol
 * (such as TCP) is used as transport, the header field MUST be used.
 * <p>
 * The size of the message-body does not include the CRLF separating header
 * fields and body.  Any Content-Length greater than or equal to zero is a
 * valid value.  If no body is present in a message, then the Content-Length
 * header field value MUST be set to zero.
 *
 * @see javax.sip.header.ContentDispositionHeader
 * @see javax.sip.header.ContentTypeHeader
 * @see javax.sip.header.ContentEncodingHeader
 * @see javax.sip.header.ContentLanguageHeader
 *
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class ContentLengthHeaderImpl extends HeaderBase implements ContentLengthHeader {
    private int contentLength;

    private final static ContentLengthHeaderParser parser = new ContentLengthHeaderParser();

    protected HeaderParser getParser() {
        return parser;
    }

    public ContentLengthHeaderImpl() {
        super(ContentLengthHeader.NAME);
    }

    ContentLengthHeaderImpl(byte[] data, int start, int end) {
        super(ContentLengthHeader.NAME, data, start, end);
    }

    public Object clone() {
        final ContentLengthHeaderImpl header = new ContentLengthHeaderImpl();
        header.cloneBase(this);
        if (parsed) {
            header.contentLength = this.contentLength;
        }
        return header;
    }

    protected String getValueAsString() {
        return Integer.toString(contentLength);
    }

    /**
     * Set content-length of ContentLengthHeader. The content-length must be
     * <p/>
     * greater than or equal to zero.
     *
     * @param contentLength the content-length of the message body
     *                      <p/>
     *                      as a decimal number of octets.
     * @throws javax.sip.InvalidArgumentException
     *          if contentLength is less than zero.
     */

    public void setContentLength(int contentLength) throws InvalidArgumentException {
        parse();
        this.contentLength = contentLength;
        invalidateHeaderData();
    }

    /**
     * Gets content-length of the message body.
     *
     * @return content-length of the message body as a decimal number of octets.
     */

    public int getContentLength() {
        parse();
        return contentLength;
    }
}
/*
 * $Log$
 */